﻿using System;
using System.Web;
using System.Collections.Generic;

public static class BBS
{
	public static String AddTopic(HttpRequest request)
	{
		String title = (String)request.Form["title"];
		String content = (String)request.Form["textC"];
		if (title == null || content == null
			|| title == "" || content == "")
			return "标题与内容均不能为空！";
		SE6.User usr = SessionMan.GetLoginUser();
		if (usr != null && !usr.CanLeaveWord())
			return "留言失败，您已被管理员禁止发言！";
		String author = (usr != null) ? usr.Name : request.UserHostAddress;
		Topic topic = new Topic(title, author);
		Reply reply = new Reply(author, 0, title, content);
		// 		if (title == null || content == null ||
		// 			title == "" || content == "")
		// 			return "主题或内容不能为空"
		if (DAMS.DataAccess.Insert(topic, reply))
			return "";
		else
			return "留言失败，请稍候再试！";

	}
	public static String AddReply(String title,String content,HttpRequest request)
	{
		SE6.User usr = SessionMan.GetLoginUser();
		if (usr != null && !usr.CanLeaveWord())
			return "发表回复失败，您已被管理员禁止发言！";
		String author = (usr != null) ? usr.Name : request.UserHostAddress;
		Int32 topicID = Convert.ToInt32(request.Form["topicID"]);
		Reply reply = new Reply(author, topicID, title, content);
		if (DAMS.DataAccess.Insert(reply))
			return "";
		else
			return "发表回复失败，请稍候再试！";
	}
	public static String ViewTopic(HttpRequest request)
	{
		const Int32 COUNT_PER_PAGE = 10;
		const Int32 PAGE_NAV_COUNT = 10;
		const Int32 HALF_PAGE_NAV_COUNT = PAGE_NAV_COUNT / 2;
 		Int32 curPage;
 		if (!Int32.TryParse(Convert.ToString(request.QueryString["cp"]) ,out curPage))
 			curPage=0;
		//Get Total Count
		Int32 totalCount = DAMS.DataAccess.GetTopicsCount();
		if (totalCount > 0)
		{
			Int32 maxPageIndex = totalCount / COUNT_PER_PAGE;
			if (curPage < 0)
				curPage = 0;
			else if (curPage > maxPageIndex)
				curPage = maxPageIndex;
			List<Topic> topics = DAMS.DataAccess.GetTopics(
				curPage * COUNT_PER_PAGE,
				COUNT_PER_PAGE);
			if (topics != null)
			{
				Int32 curCount = topics.Count;
				//生成分页导航
				String sbNav = PageNav.GenNav(totalCount, COUNT_PER_PAGE,
					curPage, PAGE_NAV_COUNT, "?");
				System.Text.StringBuilder sbResult = new System.Text.StringBuilder();
				sbResult.Append(sbNav);
				sbResult.Append("<table cellpadding='0' cellspacing='0'>");
				sbResult.Append(Topic.RenderHeader());
				Boolean odd = false;
				foreach (Topic topic in topics)
				{
					sbResult.Append(topic.Render(odd));
					odd = !odd;
				}
				sbResult.Append("</table>");
				sbResult.Append("<div>共有主题数<span class='cue'>");
				sbResult.Append(totalCount.ToString());
				sbResult.Append("</span>个。以上是第<span class='cue'>");
				sbResult.Append((curPage * COUNT_PER_PAGE + 1).ToString());
				sbResult.Append("—");
				sbResult.Append((curPage * COUNT_PER_PAGE + curCount).ToString());
				sbResult.Append("</span>个</div>");
				sbResult.Append(sbNav);
				return sbResult.ToString();
			}
		}
		return "<span class='hint'>当前尚无任何主贴。但，您的一句废话也许将载入史册。</span>";
	}
	public static String ViewReply(HttpRequest request)
	{
		const Int32 COUNT_PER_PAGE = 10;
		const Int32 PAGE_NAV_COUNT = 10;
		//const Int32 HALF_PAGE_NAV_COUNT = PAGE_NAV_COUNT / 2;
		Int32 topicID;
		if (Int32.TryParse(Convert.ToString(request["id"]), out topicID))
		{
			Int32 curPage;
			if (!Int32.TryParse(Convert.ToString(request.QueryString["cp"]), out curPage))
				curPage = 0;
			//Get Total Count
			Int32 totalCount = DAMS.DataAccess.GetReplysCount(topicID);
			if (totalCount > 0)
			{
				Int32 maxPageIndex = totalCount / COUNT_PER_PAGE;
				if (curPage < 0)
					curPage = 0;
				else if (curPage > maxPageIndex)
					curPage = maxPageIndex;
				List<Reply> replys = DAMS.DataAccess.GetReplys(
					topicID,
					curPage * COUNT_PER_PAGE,
					COUNT_PER_PAGE);
				if (replys != null)
				{
					Int32 curCount = replys.Count;
					//生成分页导航
					String sbNav = PageNav.GenNav(totalCount, COUNT_PER_PAGE,
						curPage, PAGE_NAV_COUNT, "?id="+topicID+"&");
					System.Text.StringBuilder sbResult = new System.Text.StringBuilder();
					sbResult.Append(sbNav);
					Boolean odd = false;
					//渲染当前页面的所有回复
					foreach (Reply reply in replys)
					{
						sbResult.Append(reply.Render(odd));
						odd = !odd;
					}
					sbResult.Append("<div>共有回复数<span class='cue'>");
					sbResult.Append(totalCount.ToString());
					sbResult.Append("</span>个。以上是第<span class='cue'>");
					sbResult.Append((curPage * COUNT_PER_PAGE + 1).ToString());
					sbResult.Append("—");
					sbResult.Append((curPage * COUNT_PER_PAGE + curCount).ToString());
					sbResult.Append("</span>个</div>");
					sbResult.Append(sbNav);
					return sbResult.ToString();
				}
			}
		}
		return "<span class='hint'>该帖不存在，已被删除。</span>";
	}
	public static String ChangeTopicTopMode(HttpRequest request)
	{
		SE6.User logUsr = SessionMan.GetLoginUser();
		if (logUsr == null)
			return "请先登录！";
		if (!logUsr.IsLeaveWordManager())
			return "对于该操作，您尚无足够权限！";
		Int32 id;
		if (!Int32.TryParse(request.QueryString["id"], out id))
			return "请先选择要进行操作的帖子";
		Topic topic = DAMS.DataAccess.GetTopic(id);
		if (topic == null)
			return "该帖不存在，或已被删除！";
		topic.ChangeTop();
		if (DAMS.DataAccess.UpdateTopic(topic))
			return "操作成功！";
		return "操作失败，请稍候再试！";
	}
	public static String ChangeTopicExcellentMode(HttpRequest request)
	{
		SE6.User logUsr = SessionMan.GetLoginUser();
		if (logUsr == null)
			return "请先登录！";
		if (!logUsr.IsLeaveWordManager())
			return "对于该操作，您尚无足够权限！";
		Int32 id;
		if (!Int32.TryParse(request.QueryString["id"], out id))
			return "请先选择要进行操作的帖子";
		Topic topic = DAMS.DataAccess.GetTopic(id);
		if (topic == null)
			return "该帖不存在，或已被删除！";
		topic.ChangeExcellent();
		if (DAMS.DataAccess.UpdateTopic(topic))
			return "操作成功！";
		return "操作失败，请稍候再试！";
	}
	public static String DeleteTopic(HttpRequest request)
	{
		SE6.User logUsr = SessionMan.GetLoginUser();
		if (logUsr == null)
			return "请先登录！";
		if (!logUsr.IsLeaveWordManager())
			return "对于该操作，您尚无足够权限！";
		Int32 id;
		if (!Int32.TryParse(request.QueryString["id"], out id))
			return "请先选择要进行操作的帖子";
		if (DAMS.DataAccess.DeleteTopic(id))
			return "删除帖子成功！";
		return "删除操作失败，请稍候再试！";
	}
}
